
package com.wyzz.hr.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import org.apache.commons.lang.StringUtils;

import com.wyzz.hr.base.BaseAction;
import com.wyzz.hr.base.DWRUtil;
import com.wyzz.hr.bo.IAttdMachineBO;
import com.wyzz.hr.bo.IContractTypeBo;
import com.wyzz.hr.bo.IDepartmentBO;
import com.wyzz.hr.bo.IEcptypeBO;
import com.wyzz.hr.bo.IEmpTypeBO;
import com.wyzz.hr.bo.IEmployeeBo;
import com.wyzz.hr.bo.IInfoBO;
import com.wyzz.hr.bo.IJobTitleBo;
import com.wyzz.hr.bo.IJobgradeBO;
import com.wyzz.hr.bo.ILeavetypeBO;
import com.wyzz.hr.bo.ILocationBO;
import com.wyzz.hr.bo.IOvertimetypeBo;
import com.wyzz.hr.bo.IStatBO;
import com.wyzz.hr.pojo.AttdMachine;
import com.wyzz.hr.pojo.ContractType;
import com.wyzz.hr.pojo.Department;
import com.wyzz.hr.pojo.Ecptype;
import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.pojo.Emptype;
import com.wyzz.hr.pojo.Infoclass;
import com.wyzz.hr.pojo.JobTitle;
import com.wyzz.hr.pojo.Jobgrade;
import com.wyzz.hr.pojo.Leavetype;
import com.wyzz.hr.pojo.Location;
import com.wyzz.hr.pojo.Overtimetype;
import com.wyzz.hr.pojo.Statusconf;
import com.wyzz.hr.pojo.StatusconfPK;
import com.wyzz.hr.spring.SpringBeanFactory;

public class BasicConfigAction extends BaseAction {

	public BasicConfigAction() {
	}

	private boolean checkAuthForSystem() {
		String flt = DWRUtil.checkAuth("config", "execute");
		return !"error".equalsIgnoreCase(flt);
	}

	private boolean checkAuthForComp() {
		String flt = DWRUtil.checkAuth("salaryRatingConfig", "execute");
		return !"error".equalsIgnoreCase(flt);
	}

	private boolean checkAuthForEmployee() {
		String flt = DWRUtil.checkAuth("profileConfig", "execute");
		return !"error".equalsIgnoreCase(flt);
	}

	public String refreshEmptype(String id) {
		String emptype[] = { "empType" };
		IEmployeeBo empBo = (IEmployeeBo) SpringBeanFactory.getBean("empBo");
		Employee employee = empBo.loadEmp(id, emptype);
		if (employee != null) {
			String type = employee.getEmpType().getId();
			return type;
		} else {
			return null;
		}
	}

	public String addEmpType(Emptype emp) {
		if (!checkAuthForEmployee())
			return "noauth";
		try {
			IEmpTypeBO bo = (IEmpTypeBO) SpringBeanFactory.getBean("emptypeBO");
			if (bo.addEmptype(emp))
				return emp.getId();
		} catch (Exception e) {
			return "FAIL";
		}
		return "EXISTED";
	}

	public String delEmpType(String id) {
		if (!checkAuthForEmployee())
			return "noauth";
		try {
			IEmpTypeBO bo = (IEmpTypeBO) SpringBeanFactory.getBean("emptypeBO");
			if (bo.delEmptype(com.wyzz.hr.pojo.Emptype.class, id))
				return "SUCC";
		} catch (Exception e) {
			return "ERROR";
		}
		return "FAIL";
	}

	public String updateEmpType(Emptype emptype) {
		if (!checkAuthForEmployee())
			return "noauth";
		String flag;
		try {
			IEmpTypeBO bo = (IEmpTypeBO) SpringBeanFactory.getBean("emptypeBO");
			flag = bo.updateEmptype(emptype);
			if ("0".equals(flag))
				return "SUCC";
		} catch (Exception e) {
			return "FAIL";
		}
		if ("1".equals(flag))
			return "DISABLEDERROR";
		return "EXISTED";
	}

	public String saveEmpTypeIdByBatch(String ids[]) {
		IEmpTypeBO bo = (IEmpTypeBO) SpringBeanFactory.getBean("emptypeBO");
		bo.saveEmpTypeIdByBatch(ids);
		return "SUCC";
	}

	public String saveEcpTypeByBatch(String ids[]) {
		IEcptypeBO bo = (IEcptypeBO) SpringBeanFactory.getBean("ecptypeBO");
		bo.saveEcpTypeByBatch(ids);
		return "SUCC";
	}

	public String addLocation(Location location) {
		if (!checkAuthForEmployee())
			return "noauth";
		try {
			ILocationBO localbo = (ILocationBO) SpringBeanFactory
					.getBean("locationBO");
			if (localbo.addLocation(location))
				return location.getId();
		} catch (Exception e) {
			return "FAIL";
		}
		return "EXISTED";
	}

	public String delLocation(String id) {
		if (checkAuthForEmployee()) {
			ILocationBO localbo = (ILocationBO) SpringBeanFactory
					.getBean("locationBO");
			return localbo.delLocation(com.wyzz.hr.pojo.Location.class, id);
		} else {
			return "noauth";
		}
	}

	public String saveLocationSortIdByBatch(String ids[]) {
		if (checkAuthForEmployee())
			try {
				ILocationBO localbo = (ILocationBO) SpringBeanFactory
						.getBean("locationBO");
				localbo.saveLocationSortIdByBatch(ids);
				return "SUCC";
			} catch (Exception e) {
				return "FAIL";
			}
		else
			return "noauth";
	}

	public String updateLocationSwapSortedId(String tr1Id, String tr2Id) {
		if (checkAuthForEmployee())
			try {
				ILocationBO localbo = (ILocationBO) SpringBeanFactory
						.getBean("locationBO");
				localbo.updateLocationSwapSortedId(tr1Id, tr2Id);
				return "SUCC";
			} catch (Exception e) {
				return "FAIL";
			}
		else
			return "noauth";
	}

	public String updateLocation(Location location) {
		if (!checkAuthForEmployee())
			return "noauth";
		String flag;
		try {
			ILocationBO localbo = (ILocationBO) SpringBeanFactory
					.getBean("locationBO");
			flag = localbo.updateLocation(location);
			if ("0".equals(flag))
				return "SUCC";
		} catch (Exception e) {
			return "FAIL";
		}
		if ("1".equals(flag))
			return "DISABLEDERROR";
		return "EXISTED";
	}

	public String addAttdMachine(AttdMachine attdMachine) {
		if (!checkAuthForEmployee())
			return "noauth";
		try {
			IAttdMachineBO localbo = (IAttdMachineBO) SpringBeanFactory
					.getBean("attdMachineBO");
			if (localbo.addAttdMachine(attdMachine))
				return attdMachine.getMacId();
		} catch (Exception e) {
			return "FAIL";
		}
		return "EXISTED";
	}

	public String delAttdMachine(String id) {
		if (checkAuthForEmployee()) {
			IAttdMachineBO localbo = (IAttdMachineBO) SpringBeanFactory
					.getBean("attdMachineBO");
			return localbo.delAttdMachine(com.wyzz.hr.pojo.AttdMachine.class,
					id);
		} else {
			return "noauth";
		}
	}

	public String saveAttdMachineSortIdByAttdMachine(String ids[]) {
		if (checkAuthForEmployee())
			try {
				IAttdMachineBO localbo = (IAttdMachineBO) SpringBeanFactory
						.getBean("attdMachineBO");
				localbo.saveAttdMachineSortIdByAttdMachine(ids);
				return "SUCC";
			} catch (Exception e) {
				return "FAIL";
			}
		else
			return "noauth";
	}

	public String updateAttdMachine(AttdMachine attdMachine) {
		if (!checkAuthForEmployee())
			return "noauth";
		String flag;
		try {
			IAttdMachineBO localbo = (IAttdMachineBO) SpringBeanFactory
					.getBean("attdMachineBO");
			flag = localbo.updateAttdMachine(attdMachine);
			if ("0".equals(flag))
				return "SUCC";
		} catch (Exception e) {
			return "FAIL";
		}
		if ("1".equals(flag))
			return "DISABLEDERROR";
		return "EXISTED";
	}

	public String addDepartment(Department dept) {
		if (!checkAuthForEmployee())
			return "noauth";
		try {
			IDepartmentBO deptbo = (IDepartmentBO) SpringBeanFactory
					.getBean("departmentBO");
			if (deptbo.addDepartment(dept))
				return dept.getId();
		} catch (Exception e) {
			return "FAIL";
		}
		return "EXISTED";
	}

	public String delDepartment(String id) {
		if (checkAuthForEmployee()) {
			IDepartmentBO deptbo = (IDepartmentBO) SpringBeanFactory
					.getBean("departmentBO");
			return deptbo.delDepartment(com.wyzz.hr.pojo.Department.class, id);
		} else {
			return "noauth";
		}
	}

	public String saveDepartmentSortIdByBatch(String ids[]) {
		if (checkAuthForEmployee())
			try {
				IDepartmentBO deptbo = (IDepartmentBO) SpringBeanFactory
						.getBean("departmentBO");
				deptbo.saveDepartmentSortIdByBatch(ids);
				return "SUCC";
			} catch (Exception e) {
				return "FAIL";
			}
		else
			return "noauth";
	}

	public String updateDepartment(Department dept) {
		if (!checkAuthForEmployee())
			return "noauth";
		String result;
		IDepartmentBO deptbo = (IDepartmentBO) SpringBeanFactory
				.getBean("departmentBO");
		result = deptbo.updateDepartment(dept);
		if (result.equals("success"))
			return "SUCC";
		if (result.equals("exist"))
			return "EXISTED";
		if (result.equals("error1"))
			return "DISABLEDERROR";
		if (result.equals("error2"))
			return "DISABLEDERROR2";
		try {
			return "FAIL";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "FAIL";
	}

	public String addStatus(Statusconf stat) {
		if (!checkAuthForSystem())
			return "noauth";
		try {
			IStatBO statbo = (IStatBO) SpringBeanFactory.getBean("statBO");
			if (statbo.addStat(stat))
				return String.valueOf(stat.getId().getStatusconfNo());
		} catch (Exception e) {
			return "FAIL";
		}
		return "EXISTED";
	}

	public String delStatus(StatusconfPK id) {
		if (!checkAuthForSystem())
			return "noauth";
		try {
			IStatBO statbo = (IStatBO) SpringBeanFactory.getBean("statBO");
			if (statbo.delStat(com.wyzz.hr.pojo.Statusconf.class, id))
				return "SUCC";
		} catch (Exception e) {
			return "ERROR";
		}
		return "EXISTED";
	}

	public String updateStatus(Statusconf stat) {
		if (!checkAuthForSystem())
			return "noauth";
		try {
			IStatBO statbo = (IStatBO) SpringBeanFactory.getBean("statBO");
			if (statbo.updateStatus(stat))
				return "SUCC";
		} catch (Exception e) {
			return "FAIL";
		}
		return "EXISTED";
	}

	public List findAlldept() {
		if (checkAuthForEmployee())
			try {
				IDepartmentBO deptbo = (IDepartmentBO) SpringBeanFactory
						.getBean("departmentBO");
				return deptbo.FindEnabledDepartment();
			} catch (Exception e) {
				return null;
			}
		else
			return null;
	}

	public String addInfo(Infoclass info) {
		if (!checkAuthForSystem())
			return "noauth";
		try {
			IInfoBO infobo = (IInfoBO) SpringBeanFactory.getBean("infoBO");
			if (infobo.addInfo(info))
				return String.valueOf(info.getId());
		} catch (Exception e) {
			return "FAIL";
		}
		return "EXISTED";
	}

	public String delInfo(String id) {
		if (!checkAuthForSystem())
			return "\u65E0\u6743\u9650\u64CD\u4F5C\uFF01";
		try {
			IInfoBO infobo = (IInfoBO) SpringBeanFactory.getBean("infoBO");
			boolean rs = infobo.delInfo(com.wyzz.hr.pojo.Infoclass.class, id);
			if (rs)
				return "";
		} catch (Exception e) {
			return (new StringBuilder()).append(
					"\u7CFB\u7EDF\u5F02\u5E38\uFF1A").append(e.getMessage())
					.toString();
		}
		return "\u6709\u6570\u636E\u5173\u8054\uFF01";
	}

	public String updateInfo(Infoclass info) {
		if (!checkAuthForSystem())
			return "noauth";
		try {
			IInfoBO infobo = (IInfoBO) SpringBeanFactory.getBean("infoBO");
			if (infobo.updateInfo(info))
				return "SUCC";
		} catch (Exception e) {
			return "FAIL";
		}
		return "EXISTED";
	}

	public String saveInforclassIdByBatch(String ids[]) {
		if (checkAuthForSystem())
			try {
				IInfoBO infobo = (IInfoBO) SpringBeanFactory.getBean("infoBO");
				infobo.saveInforclassIdByBatch(ids);
				return "SUCC";
			} catch (Exception e) {
				return "FAIL";
			}
		else
			return "noauth";
	}

	public String addEcptype(Ecptype ecptype) {
		if (!checkAuthForSystem())
			return "noauth";
		IEcptypeBO ecptypebo;
		try {
			ecptypebo = (IEcptypeBO) SpringBeanFactory.getBean("ecptypeBO");
			List temp = ecptypebo.getEcptypeByName(ecptype.getEcptypeName()
					.trim());
			if (temp != null && temp.size() >= 1)
				return "EXISTED";
		} catch (Exception e) {
			return "FAIL";
		}
		if (ecptypebo.addEcptype(ecptype))
			return ecptype.getId();
		return "FAIL";
	}

	public String delEcptype(String id) {
		if (!checkAuthForSystem())
			return "noauth";
		try {
			IEcptypeBO ecptypebo = (IEcptypeBO) SpringBeanFactory
					.getBean("ecptypeBO");
			if (ecptypebo.delEcptype(com.wyzz.hr.pojo.Ecptype.class, id))
				return "SUCC";
		} catch (Exception e) {
			return "ERROR";
		}
		return "FAIL";
	}

	public String updateEcptype(Ecptype ecptype) {
		if (!checkAuthForSystem())
			return "noauth";
		IEcptypeBO ecptypebo;
		try {
			ecptypebo = (IEcptypeBO) SpringBeanFactory.getBean("ecptypeBO");
			List temp = ecptypebo.getEcptypeList(ecptype);
			if (temp != null && temp.size() >= 1)
				return "EXISTED";
		} catch (Exception e) {
			return "FAIL";
		}
		if (ecptypebo.updateEpcategory(ecptype))
			return "SUCC";
		return "EXISTED";
	}

	public String addJobgrade(Jobgrade jobgrade) {
		if (!checkAuthForComp())
			return "noauth";
		try {
			IJobgradeBO jobgradeBo = (IJobgradeBO) SpringBeanFactory
					.getBean("jobgradeBO");
			if (jobgradeBo.addJobgrade(jobgrade))
				return jobgrade.getId();
		} catch (Exception e) {
			return "FAIL";
		}
		return "EXISTED";
	}

	public String delJobgrade(String id) {
		if (!checkAuthForComp())
			return "noauth";
		try {
			IJobgradeBO jobgradeBo = (IJobgradeBO) SpringBeanFactory
					.getBean("jobgradeBO");
			if (jobgradeBo.delJobgrade(com.wyzz.hr.pojo.Jobgrade.class, id))
				return "SUCC";
		} catch (Exception e) {
			return "ERROR";
		}
		return "FAIL";
	}

	public String updateJobgrade(Jobgrade jobgrade) {
		if (!checkAuthForComp())
			return "noauth";
		try {
			IJobgradeBO jobgradeBo = (IJobgradeBO) SpringBeanFactory
					.getBean("jobgradeBO");
			if (jobgradeBo.updateJobgrade(jobgrade))
				return "SUCC";
		} catch (Exception e) {
			return "FAIL";
		}
		return "EXISTED";
	}

	public String saveJobGradeIdByBatch(String ids[]) {
		if (checkAuthForComp()) {
			IJobgradeBO jobgradeBo = (IJobgradeBO) SpringBeanFactory
					.getBean("jobgradeBO");
			jobgradeBo.saveJobGradeIdByBatch(ids);
			return "SUCC";
		} else {
			return "noauth";
		}
	}

	public String addLeaveType(Leavetype leaveType) {
		if (!checkAuthForSystem())
			return "noauth";
		if (StringUtils.isNotEmpty(leaveType.getBalForward()))
			try {
				leaveType.setLtBalForward((new SimpleDateFormat("yyyy-MM-dd"))
						.parse(leaveType.getBalForward()));
			} catch (ParseException e) {
				leaveType.setLtBalForward(null);
			}
		ILeavetypeBO lt_BO = (ILeavetypeBO) SpringBeanFactory
				.getBean("leavetypeBO");
		String rs = lt_BO.addLeaveType(leaveType);
		if ("SUCC".equals(rs))
			return leaveType.getLtNo();
		else
			return rs;
	}

	public String delLeaveType(String id) {
		if (!checkAuthForSystem()) {
			return "noauth";
		} else {
			ILeavetypeBO lt_BO = (ILeavetypeBO) SpringBeanFactory
					.getBean("leavetypeBO");
			return lt_BO.delLeaveType(com.wyzz.hr.pojo.Leavetype.class, id);
		}
	}

	public String saveLeavetypeByBatch(String ids[]) {
		ILeavetypeBO bo = (ILeavetypeBO) SpringBeanFactory
				.getBean("leavetypeBO");
		bo.saveLeavetypeByBatch(ids);
		return "SUCC";
	}

	public String saveOvertimetypeByBatch(String ids[]) {
		IOvertimetypeBo bo = (IOvertimetypeBo) SpringBeanFactory
				.getBean("overtimetypeBO");
		bo.saveOvertimetypeByBatch(ids);
		return "SUCC";
	}

	public String updateLeaveType(Leavetype leaveType) {
		if (!checkAuthForSystem())
			return "noauth";
		try {
			if (StringUtils.isNotEmpty(leaveType.getBalForward()))
				leaveType.setLtBalForward((new SimpleDateFormat("yyyy-MM-dd"))
						.parse(leaveType.getBalForward()));
			ILeavetypeBO lt_BO = (ILeavetypeBO) SpringBeanFactory
					.getBean("leavetypeBO");
			String rs = lt_BO.updateLeaveType(leaveType);
			return rs;
		} catch (Exception e) {
			return "FAIL";
		}
	}

	public Leavetype getLeaveTypeById(String id) {
		if (!checkAuthForSystem()) {
			return null;
		} else {
			ILeavetypeBO lt_BO = (ILeavetypeBO) SpringBeanFactory
					.getBean("leavetypeBO");
			return lt_BO.getLeavetype(id);
		}
	}

	public String addOvertimetype(Overtimetype overtimetype) {
		if (!checkAuthForSystem())
			return "noauth";
		IOvertimetypeBo ot_BO;
		try {
			ot_BO = (IOvertimetypeBo) SpringBeanFactory
					.getBean("overtimetypeBO");
			List temp = ot_BO.findByName(overtimetype.getOtName().trim());
			if (temp.size() >= 1)
				return "EXISTED";
		} catch (Exception e) {
			return "FAIL";
		}
		if (ot_BO.addOvertimetype(overtimetype))
			return overtimetype.getOtNo();
		return "FAIL";
	}

	public String delOvertimetype(String id) {
		if (!checkAuthForSystem())
			return "noauth";
		IOvertimetypeBo ot_BO;
		try {
			ot_BO = (IOvertimetypeBo) SpringBeanFactory
					.getBean("overtimetypeBO");
			if ("01".equals(id))
				return "01";
		} catch (Exception e) {
			return "ERROR";
		}
		if ("02".equals(id))
			return "02";
		if ("03".equals(id))
			return "03";
		if (ot_BO.delOvertimetype(com.wyzz.hr.pojo.Overtimetype.class, id))
			return "SUCC";
		return "FAIL";
	}

	public String updateOvertimetype(Overtimetype overtimetype) {
		if (!checkAuthForSystem())
			return "noauth";
		IOvertimetypeBo ot_BO;
		try {
			ot_BO = (IOvertimetypeBo) SpringBeanFactory
					.getBean("overtimetypeBO");
			List temp = ot_BO.findOvertimetypeList(overtimetype);
			if (temp.size() >= 1)
				return "EXISTED";
		} catch (Exception e) {
			return "FAIL";
		}
		if (ot_BO.updateOvertimetype(overtimetype))
			return "SUCC";
		return "FAIL";
	}

	public String addContractType(ContractType conType) {
		if (checkAuthForSystem()) {
			IContractTypeBo conBo = (IContractTypeBo) SpringBeanFactory
					.getBean("contractTypeBo");
			List temp = conBo.findByName(conType.getEctName());
			if (temp != null) {
				int size = temp.size();
				if (size > 1)
					return "EXISTED";
				if (size == 1
						&& !conType.getId().equalsIgnoreCase(
								((ContractType) temp.get(0)).getId()))
					return "EXISTED";
			}
			conBo.addContractType(conType);
			return conType.getId();
		} else {
			return "noauth";
		}
	}

	public String deleteContractType(String id) {
		if (checkAuthForSystem()) {
			IContractTypeBo conBo = (IContractTypeBo) SpringBeanFactory
					.getBean("contractTypeBo");
			return conBo.delContractType(id);
		} else {
			return "noauth";
		}
	}

	public String updateContractType(ContractType conType) {
		if (checkAuthForSystem()) {
			IContractTypeBo conBo = (IContractTypeBo) SpringBeanFactory
					.getBean("contractTypeBo");
			List temp = conBo.findByName(conType.getEctName());
			if (temp != null) {
				int size = temp.size();
				if (size > 1)
					return "EXISTED";
				if (size == 1
						&& !conType.getId().equalsIgnoreCase(
								((ContractType) temp.get(0)).getId()))
					return "EXISTED";
			}
			conBo.updateContractType(conType);
			return "SUCC";
		} else {
			return "noauth";
		}
	}

	public String saveContractIdByBatch(String ids[]) {
		if (checkAuthForEmployee()) {
			IContractTypeBo conBo = (IContractTypeBo) SpringBeanFactory
					.getBean("contractTypeBo");
			try {
				conBo.saveContractTypeIdByBatch(ids);
				return "SUCC";
			} catch (Exception ex) {
				ex.printStackTrace();
			}
			return "FAIL";
		} else {
			return "noauth";
		}
	}

	public String addJobTitle(JobTitle jobTitle) {
		IJobTitleBo jobTitleBo;
		if (!checkAuthForEmployee())
			return "noauth";
		jobTitleBo = (IJobTitleBo) SpringBeanFactory.getBean("jobTitleBo");
		if (jobTitleBo.addJobTitle(jobTitle))
			return jobTitle.getJobtitleNo();
		try {
			return "EXISTED";
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return "ERROR";
	}

	public String delJobTitle(String id) {
		if (!checkAuthForEmployee())
			return "noauth";
		IJobTitleBo jobTitleBo = (IJobTitleBo) SpringBeanFactory
				.getBean("jobTitleBo");
		if (jobTitleBo.delJobTitle(com.wyzz.hr.pojo.JobTitle.class, id))
			return "SUCC";
		try {
			return "FAIL";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "ERROR";
	}

	public String updateJobTitle(JobTitle jobTitle) {
		if (!checkAuthForEmployee())
			return "noauth";
		String flag;
		IJobTitleBo jobTitleBo = (IJobTitleBo) SpringBeanFactory
				.getBean("jobTitleBo");
		flag = jobTitleBo.updateJobTitle(jobTitle);
		if ("0".equals(flag))
			return "SUCC";
		if ("1".equals(flag))
			return "DISABLEDERROR";
		try {
			return "EXISTED";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "FAIL";
	}

	public String saveJobTitleSortIdByBatch(String ids[]) {
		if (checkAuthForEmployee()) {
			try {
				IJobTitleBo jobTitleBo = (IJobTitleBo) SpringBeanFactory
						.getBean("jobTitleBo");
				jobTitleBo.saveJobTitleSortIdByBatch(ids);
				return "SUCC";
			} catch (Exception e) {
				e.printStackTrace();
			}
			return "FAIL";
		} else {
			return "noauth";
		}
	}

	private static final String ERROR = "ERROR";
	private static final String EXISTED = "EXISTED";
	private static final String SUCC = "SUCC";
	private static final String FAIL = "FAIL";
	private static final String DISABLEDERROR = "DISABLEDERROR";
}
